<template>
  <div class="dashboard-editor-container">
    <github-corner class="github-corner" />

    <el-row style="margin-bottom: 32px;">
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <panel-group
          :counts="counts"
          @handleSetPieChartData="handleSetPieChartData"
        />
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="12">
        <div class="chart-wrapper">
          <pie-chart ref="pieChart" />
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="24">
        <div class="chart-wrapper">
          <gps-chart ref="gpsChart" :gps-data="gpsData" />
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="24">
        <div class="chart-wrapper">
          <line-chart />
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="8">
      <el-col
        :xs="{span: 24}"
        :sm="{span: 24}"
        :md="{span: 24}"
        :lg="{span: 24}"
        :xl="{span: 12}"
        style="padding-right:8px;margin-bottom:30px;"
      >
        <transaction-table />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import GithubCorner from '@/components/GithubCorner'
import PanelGroup from './components/PanelGroup'
import PieChart from './components/PieChart' // 饼图
import GpsChart from '@/views/dashboard/admin/components/GpsChart' // gps图
import LineChart from '@/views/dashboard/admin/components/LineChart' // 折线图
import TransactionTable from './components/TransactionTable' // 报警数前10的设备指标报警表
import { getAllGps } from '@/api/device-jps'

export default {
  name: 'DashboardAdmin',
  components: {
    GithubCorner,
    PanelGroup,
    PieChart,
    GpsChart,
    LineChart,
    TransactionTable
  },
  data() {
    return {
      counts: { all: 0, normal: 0, status: 0, online: 0, alarm: 0 },
      rawPieData: [],
      gpsData: {},
      countOptions: {}
    }
  },
  created() {
    this.handleRefresh()
    // getDeviceStatusChart().then(response => {
    //   for (const ele of response.data.list) {
    //     this.counts[ele.name] = ele.value
    //     this.rawPieData.push(ele)
    //     this.handleSetPieChartData('alarm')
    //   }
    // })
  },
  methods: {
    handleRefresh() {
      getAllGps().then(resp => {
        this.gpsData = resp.data.gps
        this.counts.all = this.gpsData.total
        this.counts.normal = this.gpsData.normal
        this.counts.status = this.gpsData.status
        this.counts.online = this.gpsData.online
        this.counts.alarm = this.gpsData.alarm
        this.handleSetPieChartData('alarm')
        this.$refs.gpsChart.handleRefresh(this.gpsData)
        // console.log(this.gpsData, this.counts)
      })
    },
    handleSetPieChartData(type) {
      const all = this.counts['all']
      const val = this.counts[type]
      let legendData, countList
      // console.log('type', type, countList)
      if (type === 'alarm') {
        legendData = ['发出报警设备', '未报警设备']
        countList = [val, all - val]
      } else if (type === 'status') {
        legendData = ['开启设备', '关闭设备']
        countList = [val, all - val]
      } else if (type === 'online') {
        legendData = ['在线设备', '离线设备']
        countList = [val, all - val]
      } else if (type === 'all') {
        legendData = ['所有设备']
        countList = [all]
      } else {
        legendData = ['无']
        countList = [0]
      }
      this.$refs.pieChart.handleRefresh(legendData, countList)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .github-corner {
    position: absolute;
    top: 0;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
  }
}

</style>
